// 链接地址
// var issOnlineUrl = "http://127.0.0.1:22001/ZKBIOOnline";
var issOnlineUrl = "https://127.0.0.1:22002/ZKBIOOnline";
var isComp= false;  //是否为对比功能

let timeOut = null

var canvas = null
var ctx = null

var fingerimgurl

var autographPath
function getWebServerInfo(dom,src)
{
    canvas = dom
    // autographPath = src
	ajaxs(issOnlineUrl+"/info").then(res=>{
        if(res.ret==0){
            pullFingerFile(dom)
        }
    }).catch(()=>{
        cancelRegister()
    })
}

//获取指纹图片
function restursImg(){
    ajaxs(issOnlineUrl+"/fingerprint/getImage").then(res=>{
        if(res.ret==0){
            clearInterval(timeOut)
            var fingerimg = new Image(); 
            fingerimg.src = ''
            fingerimg.src = "data:image/jpg;base64,"+res.data.jpg_base64
            fingerimgurl = fingerimg.src
            fingerimg.onload = function(){
                ctx=canvas.getContext("2d");
                ctx.drawImage(fingerimg,0,0,canvas.width ,canvas.height);
                var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
                var length = imageData.data.length;
                for (var index = 0; index < length; index += 4) {
                    var r = imageData.data[index];
                    var g = imageData.data[index + 1];
                    var b = imageData.data[index + 2];
                    if(r>=223&&r!=255&&g>=223&&g!=255&&b>=223&&b!=255){
                        // 这里可以对 r g b 进行计算（这里的rgb是每个像素块的rgb颜色）
                        imageData.data[index] = 255;
                        imageData.data[index + 1] = 255;
                        imageData.data[index + 2] = 255;
                        imageData.data[index + 3] = 0;
                    }
                }
                ctx.putImageData(imageData, 0, 0);
                
                // ctx.drawImage(autographPath,0,0,canvas.width ,canvas.height);
            }
        }
    })
}

function serveImgPng(){
    if(fingerimgurl){
        return canvas.toDataURL("image/png")
    }else{
        return null
    }
    return fingerimgurl
}
//开始录入
function pullFingerFile(){
    ajaxs(issOnlineUrl+"/fingerprint/beginCapture?type=1&FakeFunOn=0").then(res=>{
        if(res.ret==0){
            timeOut = setInterval(()=>{
                restursImg()
            },200)
        }
    })
}
//取消采集
function cancelRegister(){
    fingerimgurl=null
    clearInterval(timeOut)
    ajaxs(issOnlineUrl+"/fingerprint/cancelCapture").then(res=>{
        console.log(canvas,'666');
        if(ctx){
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }
    })
    
}


function ajaxs (url, data) {
    return new Promise((resolve,reject)=>{
        var params = '';
        if(data){
            Object.keys(data).forEach((item)=>{
                params += '&'+data[item]
            })
            params = params.replace('&',"?")
        }
        const request = new XMLHttpRequest();
        request.open(
          "get",
          url+params,
          true
        );
        request.send();
        request.onloadend = (res)=>{
            const XMLHttp = res.currentTarget
            if(XMLHttp.readyState === 4 && XMLHttp.status === 200){
                resolve(JSON.parse(XMLHttp.response))
            }else{
                reject('请求失败')
            }
        }
    })
}


export default {
    getWebServerInfo, //初始化指纹设备
    cancelRegister,  // 取消指纹采集
    pullFingerFile,  // 开启指纹采集

    serveImgPng,// 指纹图片
}